#ifndef UPDATECHECKER_H
#define UPDATECHECKER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QDebug>

class UpdateChecker : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString currentVersion READ currentVersion CONSTANT)
    Q_PROPERTY(QString latestVersion READ latestVersion NOTIFY latestVersionChanged)
    Q_PROPERTY(QString releaseNotes READ releaseNotes NOTIFY releaseNotesChanged)
    Q_PROPERTY(bool forceUpdate READ forceUpdate NOTIFY forceUpdateChanged)
    Q_PROPERTY(bool isChecking READ isChecking NOTIFY isCheckingChanged)
    Q_PROPERTY(bool isDownloading READ isDownloading NOTIFY isDownloadingChanged)
    Q_PROPERTY(int downloadProgress READ downloadProgress NOTIFY downloadProgressChanged)
    Q_PROPERTY(bool overwriteExisting READ overwriteExisting WRITE setOverwriteExisting NOTIFY overwriteExistingChanged)

public:
    explicit UpdateChecker(QObject *parent = nullptr);
    ~UpdateChecker();

    QString currentVersion() const { return "1.0.0"; }  // 当前版本号
    QString latestVersion() const { return m_latestVersion; }
    QString releaseNotes() const { return m_releaseNotes; }
    bool forceUpdate() const { return m_forceUpdate; }
    bool isChecking() const { return m_isChecking; }
    bool isDownloading() const { return m_isDownloading; }
    int downloadProgress() const { return m_downloadProgress; }
    bool overwriteExisting() const { return m_overwriteExisting; }
    void setOverwriteExisting(bool overwrite) {
        if (m_overwriteExisting != overwrite) {
            m_overwriteExisting = overwrite;
            emit overwriteExistingChanged(overwrite);
        }
    }

public slots:
    void checkForUpdates();
    void downloadUpdate();
    void cancelDownload();

signals:
    void updateAvailable(const QString &version);
    void noUpdateAvailable();
    void updateError(const QString &error);
    void updateFinished();
    void latestVersionChanged(const QString &version);
    void releaseNotesChanged(const QString &notes);
    void forceUpdateChanged(bool force);
    void isCheckingChanged(bool checking);
    void isDownloadingChanged(bool downloading);
    void downloadProgressChanged(int progress);
    void overwriteExistingChanged(bool overwrite);

private slots:
    void onCheckFinished();
    void onDownloadFinished();
    void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

private:
    QNetworkAccessManager *m_manager;
    QNetworkReply *reply;
    QString m_latestVersion;
    QString m_releaseNotes;
    bool m_forceUpdate;
    bool m_isChecking;
    bool m_isDownloading;
    int m_downloadProgress;
    bool m_overwriteExisting;
    QString m_updateUrl;
    QString m_downloadPath;

    // 更新服务器配置
    static const QString UPDATE_SERVER_BASE_URL;
    static const QString VERSION_FILE_NAME;
};

#endif // UPDATECHECKER_H 